import 'package:flutter/material.dart';
import 'package:flutter_lang/lang_index.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'package:route_manager/wx_navigator.dart';

import '../../module_config.dart';

class QRCodeScan extends StatefulWidget {
  const QRCodeScan({
    Key key,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() => _QRCodeScanState();
}

class _QRCodeScanState extends State<QRCodeScan> {
  QRViewController controller;
  final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');

  String _qrData = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('${WXLang.$t("home_qrcode_scan", package: ModuleConfig.moduleName)}'),
        leading: IconButton(
            icon: Icon(Icons.arrow_back, size: 28),
            onPressed: () {
              Navigator.of(context).pop();
            }),
      ),
      body: Center(
          child: Stack(
        children: [
          QRView(
            key: qrKey,
            onQRViewCreated: _onQRViewCreated,
            overlay: QrScannerOverlayShape(
              borderColor: Colors.green,
              borderRadius: 0,
              borderLength: 20,
              borderWidth: 2,
              cutOutSize: 240,
            ),
          ),
          Positioned(
              child: Container(
            // margin: EdgeInsets.,
            width: double.infinity,
            height: 30,
            alignment: Alignment.center,
            child: Text(
              _qrData,
              style: TextStyle(color: Colors.white),
            ),
          ))
        ],
      )),
    );
  }

  void _onQRViewCreated(QRViewController controller) {
    this.controller = controller;
    controller.scannedDataStream.listen((scanData) {
//      setState(() {

      _qrData = scanData;
      controller.pauseCamera();
      controller.dispose();
      WXNavigator.pop(result: scanData);
//      });
    });
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}
